<?php
class Pay_method_model extends CI_Model
{
    private $table = 'pay_method';
    
    public function findShortcut($shortcut)
    {
        $this->db->where('shortcut', $shortcut);
        $this->db->order_by('sort','asc');
        return $this->db->get($this->table);
    }
    
    public function findByBankId($pay_bank)
    {
        $this->db->where('bank_id', $pay_bank);
        $result = $this->db->get($this->table);
        return $result->row();
    }
    
    public function total()
    {
        return $this->db->count_all_results($this->table);
    }
    
    public function page_list($num)
    {
        $this->db->order_by('sort','asc');
        $this->db->limit(20, $num);
        return $this->db->get($this->table);
    }
    
    public function insertPayMethod($postData, $imageData)
    {
        $data = array(
            'method_name' => $postData['method_name'],
            'bank_name' => $postData['bank_name'],
            'bank_id'    => $postData['bank_id'],
            'bank_code'    => $postData['bank_code'],
            'shortcut'    => $postData['shortcut'],
            'sort'    => $postData['sort'],
        );
        if (trim($imageData['file_name'])) {
            $data['bank_img'] = $imageData['file_name'];
        }
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function updatePayMethod($postData, $imageData)
    {
        $data = array(
            'method_name' => $postData['method_name'],
            'bank_name' => $postData['bank_name'],
            'bank_id'    => $postData['bank_id'],
            'bank_code'    => $postData['bank_code'],
            'shortcut'    => $postData['shortcut'],
            'sort'    => $postData['sort'],
        );
        if (!empty($imageData['file_name'])) {
            $data['bank_img'] = $imageData['file_name'];
        }
        $this->db->where('method_id', $postData['method_id']);
        return $this->db->update($this->table, $data);
    }
    
    public function findById($id)
    {
        $this->db->where('method_id', $id);
        return $this->db->get($this->table);
    }
    
    public function deleteByMethodId($method_id)
    {
        $this->db->where('method_id', $method_id);
        return $this->db->delete($this->table);
    }
    
    public function getAllBankId($isArray=false)
    {
        $result = $this->db->get($this->table);
        if ($isArray) {
            $rows = array();
            foreach ($result->result_array() as $row) {
                $rows[$row['bank_id']] = $row;
            }
            return $rows;
        }
        return $result;
    }
}